package com.entrogy.dao;

import com.entrogy.bean.WeixinEntity;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

@Repository
public interface WeixinMapper {

    @Insert("insert into weixin_information (openid, unionid) value (#{openid}, #{unionid})")
    int insertOpenidandUnionid(@Param("openid") String openid,@Param("unionid") String unionid);

    @Select("select * from weixin_information where openid=#{openid}")
    WeixinEntity getWeixinByOpenid(@Param("openid") String openid);

    @Update("update weixin_information set unionid=#{unionid} where openid=#{openid}")
    void updateUnionidByOpein(@Param("openid") String openid, @Param("unionid") String unionid);

    @Update("update weixin_information set sessionkey=#{code} where openid=#{openid}")
    void updateSessionKey(@Param("openid") String openid, @Param("code") String code);

    @Insert("insert into weixin_information (openid) value (#{openid})")
    void insertOpenid(@Param("openid") String openid);

    @Update("update weixin_information set uid=#{uid} where id=#{id}")
    void insertWeixinUid(@Param("id") String id, @Param("uid") String uid);

    @Select("select id from weixin_information where uid=#{uid}")
    Integer haveUnionid(@Param("uid") String uid);

    @Insert("update weixin_information set phone=#{phone} where uid=#{uid}")
    void insertVisitorPhone(@Param("uid") String uid, @Param("phone") String phone);

    @Select("select * from weixin_information where id=#{id}")
    WeixinEntity loginWeixin(@Param("id") String id);

    @Update("update  weixin_information set nickname=#{nickName}, headurl=#{headUrl} where id=#{id}")
    void updateWeixin(@Param("id") String id, @Param("nickName") String nickName,@Param("headUrl") String headUrl);

    @Select("select * from weixin_information where uid=#{uid}")
    WeixinEntity getWeixinByUid(@Param("uid") String uid);

    @Update("update weixin_information set phone=(select parent_visitor_phone from parent_visitor_information where parent_visitor_id=#{uid}) where uid=#{uid}")
    void updateParentPhone(@Param("uid") String uid);

    @Update("update weixin_information set phone=(select mobile from user where id=#{uid}) where uid=#{uid}")
    void updateTeacherPhone(@Param("uid") String uid);

    @Select("select openid from weixin_information where phone=#{phone}")
    String getOpenIdByphone(@Param("phone")String phone);

    @Update("update weixin_information set phone = #{phone} where uid = #{uid}")
    void updateWeixinPhoneByUid(@Param("phone") String phone, @Param("uid") String uid);

    @Select("select * from weixin_information where phone = #{mobile}")
    WeixinEntity getWeixinByPhone(@Param("mobile") String mobile);

    @Delete("delete from weixin_information where phone = #{mobile}")
    void deleteWeixinByPhone(@Param("mobile") String mobile);

    @Select("select phone from weixin_information where id=#{id}")
    String selectLoginPhoneWithWeiXinId( @Param(value = "id") String id);

    @Select("update weixin_information set uid=#{userId},phone=#{phone} where id=#{weiXinInfoId}")
    Integer updateWeiXinByUserId(@Param("weiXinInfoId") Integer weiXinInfoId, @Param("phone") String phone, @Param("userId") String userId);

    @Update("update weixin_information set phone = null where phone = #{phone}")
    int updateWeixinPhone(@Param("phone") String phone);
}
